package com.zx.idc.backend.gui.base.house;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zx.idc.backend.gui.base.house.service.BaseHouseInfoReportService;
import com.zx.idc.backend.gui.common.BaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.backend.gui.common.base.BaseExcelApi;
import com.zx.idc.ds.base.house.entity.BaseHouseInfo;
import com.zx.idc.ds.base.house.service.IBaseHouseInfoService;
import com.zx.idc.ds.base.house.vo.BaseHouseInfoVo;
import com.zx.idc.ds.common.constant.DataFeatureConstant;
import com.zx.idc.ds.common.constant.DelFlagConstant;
import com.zx.idc.ds.common.constant.ReportStatusConstant;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * IDC 机房信息管理 前端控制器
 * </p>
 *
 * @author rxliuli
 * @since 2019-01-31
 */
@RestController
@RequestMapping("/api/baseHouseInfo")
public class BaseHouseInfoController extends BaseApi implements BaseExcelApi<IBaseHouseInfoService, BaseHouseInfoVo> {

    @Autowired
    private IBaseHouseInfoService service;
    @Autowired
    private BaseHouseInfoReportService baseHouseInfoReportService;

    @GetMapping("/query-page")
    @RequiresPermissions("basic-data:base-house-info")
    public Result<Page<BaseHouseInfoVo>> queryPage(Page<BaseHouseInfoVo> page, BaseHouseInfoVo baseHouseInfoVo) {
        return success(service.queryPage(page, baseHouseInfoVo));
    }

    @GetMapping("/get/{id}")
    @RequiresPermissions("basic-data:base-house-info")
    public Result<BaseHouseInfoVo> get(@PathVariable("id") Long id) {
        return success(service.getById(id));
    }

    @GetMapping("/idc-id/{idcId}/house-num/{houseNum}")
    @RequiresPermissions("basic-data:base-house-info")
    public Result<Boolean> getHouseIdByIdcId(@PathVariable("idcId") String idcId, @PathVariable("houseNum") String houseNum) {
        return success(
            service.selectCount(
                new BaseHouseInfo()
                    .setIdcId(idcId)
                    .setHouseNumber(houseNum)
            ) == 0);
    }

    @GetMapping("/idc-id/{idcId}/house-name/{houseName}")
    @RequiresPermissions("basic-data:base-house-info")
    public Result<Boolean> getHouseNameByIdcId(@PathVariable("idcId") String idcId, @PathVariable("houseName") String houseName) {
        return success(
            service.selectCount(
                new BaseHouseInfo()
                    .setIdcId(idcId)
                    .setHouseName(houseName)
            ) == 0);
    }

    /**
     * 查询所属机房列表
     */
    @GetMapping("/select-list")
    @RequiresPermissions(value = {"basic-data:base-house-info"
        , "basic-data:base-frame-info"
        , "basic-data:base-ip-address-info"
        , "basic-data:base-customer"
        , "basic-data:base-monitor-device"
        , "illegal-monitoring:mo-filing-supervise"
        , "illegal-monitoring:mo-illegal-media"
        , "illegal-monitoring:mo-illegal-info"
        , "regulatory-directive:ins-admin-control"
        , "regulatory-directive:ins-accesslog-query"
        , "regulatory-directive:ins-file"
        , "regulatory-directive:ins-monitor"
        , "regulatory-directive:ins-filter"
        , "regulatory-log:slog-abnormal-ip"
        , "regulatory-log:slog-illegal-site"
        , "regulatory-log:slog-hot-domain"
        , "regulatory-log:slog-hot-ip"
        , "regulatory-log:slog-hot-url"
        , "regulatory-log:ins-accesslog-query"
        , "regulatory-log:monitor-log"
        , "regulatory-log:filter-log"
    }, logical = Logical.OR)
    public Result<List<BaseHouseInfo>> list() {
        return success(service.selectList(
            new EntityWrapper<BaseHouseInfo>().setSqlSelect("id", "houseName")
        ));
    }

    /**
     * 查询所属机房列表
     */
    @PostMapping("/select-list-param")
    @RequiresPermissions("basic-data:base-house-info")
    public Result<List<BaseHouseInfo>> listByParam(@RequestBody BaseHouseInfoVo baseHouseInfoVo) {
        return success(service.selectList(
            new EntityWrapper<BaseHouseInfo>(baseHouseInfoVo).setSqlSelect("id", "houseName")
        ));
    }

    @PostMapping("/add")
    @RequiresPermissions("basic-data:base-house-info:edit")
    public Result<Boolean> add(@RequestBody BaseHouseInfoVo entity) {
        entity.setUpdateTime(LocalDateTime.now())
            .setDelFlag(DelFlagConstant.NORMAL)
            .setReportStatus(ReportStatusConstant.NOT_REPORTED)
            .setDataFeature(DataFeatureConstant.NEW);
        String msg;
        try {
            return success(service.insertHouseInfo(entity));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("新增机房异常", e);
        }
        return error(msg);
    }

    @PostMapping("/update")
    @RequiresPermissions("basic-data:base-house-info:edit")
    public Result<Boolean> update(@RequestBody BaseHouseInfoVo entity) {
        String msg;
        try {
            return success(service.updateHouseInfo(entity));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("修改机房异常", e);
        }
        return error(msg);
    }

    @PostMapping("/delete/batch")
    @RequiresPermissions("basic-data:base-house-info:edit")
    public Result<Boolean> deleteList(@RequestBody List<Long> ids) {
        String msg;
        try {
            return success(service.deleteBatchByIds(ids));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("删除机房异常", e);
        }
        return error(msg);
    }

    /**
     * 获取一个 Service，由子类实现
     * 实现这个方法可以更快的获取到 Service 以提高响应速度
     *
     * @return 服务类实现对象
     */
    @Override
    public Optional<IBaseHouseInfoService> getService() {
        return Optional.of(service);
    }

    @PostMapping("/insert-report/{id}")
    @RequiresPermissions("basic-data:base-house-info:edit")
    public Result<Boolean> insertReport(@PathVariable Long id) {
        String msg;
        try {
            return success(baseHouseInfoReportService.insertReport(id));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("机房数据 新增上报异常", e);
        }
        return error(msg);
    }

    @PostMapping("/update-report/{id}")
    @RequiresPermissions("basic-data:base-house-info:edit")
    public Result<Boolean> updateReport(@PathVariable Long id) {
        String msg;
        try {
            return success(baseHouseInfoReportService.updateReport(id));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("机房数据 修改上报异常", e);
        }
        return error(msg);
    }
}
